import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'


Vue.use(Router)

var routes=[
  {
    path: '/',   //路径
    name: 'home',
    redirect:'/welcome',
    component: Home, //对应的组件
    children: [
      {
        path: '/welcome',
        component:()=>import('./views/index/welcome.vue')
      },
      {
        path: '/users',
        component:()=>import('./views/index/users.vue')
      },
      {
        path: '/roles',
        component:()=>import('./views/index/roles.vue')
      },
      {
        path: '/rights',
        component:()=>import('./views/index/rights.vue')
      },
      {
        path: '/categories',
        component:()=>import('./views/index/categories.vue')
      },
      {
        path: '/params',
        component:()=>import('./views/index/params.vue')
      },
      {
        path: '/goods',
        component:()=>import('./views/index/goods.vue')
      }
      ,
      {
        path: '/orders',
        component:()=>import('./views/index/orders.vue')
      },
      {
        path: '/reports',
        component:()=>import('./views/index/reports.vue')
      }
      
    ]
  },
  {
    path: "/login",
    component:()=>import('./views/login.vue')
},
  {
        path: "/about",
        component:()=>import('./views/About.vue')
  },
  {
    path: '*',   //匹配的是所有的
    redirect:'/'   //重定向到首页
  }
]



// 创建路由对象
var router= new Router({
  routes: routes
})

// 全局守卫
router.beforeEach((to, from, next) => {
  console.log(to)
  // 除了登录 都要守卫
  if (to.path == '/login') {
    next()
    return 
  }
  
  //  判断是否登录  登录直接进入   没有登录 跳转到登录页
  if (localStorage.getItem('token')) {
    next()
  } else {
    next('/login')
  }



})


export default router
